package com.guigu.service.impl.manufacture;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.mapper.manufacture.MDesignProcedureMapper;
import com.guigu.pojo.manufacture.MDesignProcedure;
import com.guigu.service.manufacture.MDesignProcedureService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr.Zhang
 * @since 2024-12-06
 */
@Service
public class MDesignProcedureServiceImpl extends ServiceImpl<MDesignProcedureMapper, MDesignProcedure> implements MDesignProcedureService {

    @Autowired
    private MDesignProcedureMapper mDesignProcedureMapper;

    @Override
    public int addProcedure(MDesignProcedure mDesignProcedure) {
        int i = mDesignProcedureMapper.insert(mDesignProcedure);
        return i;
    }

    @Override
    public MDesignProcedure getEndProcedure() {
        QueryWrapper<MDesignProcedure> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("DESIGN_ID");
        queryWrapper.last("limit 1");
        MDesignProcedure mDesignProcedure = mDesignProcedureMapper.selectOne(queryWrapper);
        return mDesignProcedure;
    }

    @Override
    public PageInfo<MDesignProcedure> selectByPage(HashMap<String, Object> map) {
        Integer pageNum = 1;
        Integer pageSize = 5;

        Object pageNumObj = map.get("pageNum");
        if (pageNumObj != null && StringUtils.isNotBlank((String) pageNumObj)) {
            pageNum = Integer.parseInt((String) pageNumObj);
        }
        Object pageSizeObj = map.get("pageSize");
        if (pageSizeObj != null && StringUtils.isNotBlank((String) pageSizeObj)) {
            pageSize = Integer.parseInt((String) pageSizeObj);
        }
        QueryWrapper<MDesignProcedure> queryWrapper = new QueryWrapper<>();
        if (map.containsKey("checkTag")){
            if (StringUtils.isNotBlank((String) map.get("checkTag")) && map.get("checkTag").toString().equals("0")) {
                queryWrapper.in("CHECK_TAG","0","2");
            }else {
                queryWrapper.in("CHECK_TAG",map.get("checkTag"));
            }
        }
        if (map.containsKey("changeTag")){
            queryWrapper.in("CHANGE_TAG",map.get("changeTag"));
        }
        if (map.containsKey("designModuleTag")){
            queryWrapper.eq("DESIGN_MODULE_TAG",map.get("designModuleTag"));
        }
        if (map.containsKey("designModuleChangeTag")){
            queryWrapper.eq("DESIGN_MODULE_CHANGE_TAG",map.get("designModuleChangeTag"));
        }
        PageHelper.startPage(pageNum,pageSize);
        List<MDesignProcedure> mDesignProcedures = mDesignProcedureMapper.selectList(queryWrapper);
        PageInfo<MDesignProcedure> pageInfo = new PageInfo<>(mDesignProcedures);
        return pageInfo;
    }

    @Override
    public MDesignProcedure selectById(Integer id) {
        MDesignProcedure mDesignProcedure = mDesignProcedureMapper.selectById(id);
        return mDesignProcedure;
    }

    @Override
    public MDesignProcedure selectByDesignId(String designId) {
        QueryWrapper<MDesignProcedure> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DESIGN_ID",designId);
        MDesignProcedure mDesignProcedure = mDesignProcedureMapper.selectOne(queryWrapper);
        return mDesignProcedure;
    }

    @Override
    public MDesignProcedure selectByproductId(String productId) {
        QueryWrapper<MDesignProcedure> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PRODUCT_ID",productId);
        MDesignProcedure mDesignProcedure = mDesignProcedureMapper.selectOne(queryWrapper);
        return mDesignProcedure;
    }
}
